﻿using SQLite;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;


namespace Db_Sqlite.dataTables
{
    /// <summary>
    /// 绑定到数据库的表,定义了数据表的结构和属性。
    /// </summary>
    [Table("DtList")]
    public sealed record DtList
    {
        private static readonly PropertyInfo[] _props;
        static DtList()
        {
            // 获取所有公共实例属性
            // 缓存属性信息（提升反射性能）
            _props = typeof(DtList).GetProperties(BindingFlags.Public | BindingFlags.Instance);
        }

        public DtList() { }
        // 构造函数
        public DtList(string[] db)
        {
            if (db == null || db.Length < 22)
                throw new ArgumentException("数组长度必须至少为22");

            // 获取所有公共实例属性
            //var properties = GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);

            for (int i = 0; i < 22; i++)
            {
                var prop = _props.FirstOrDefault(p => p.Name.Equals($"x{i + 1}", StringComparison.OrdinalIgnoreCase));
                if (prop != null && prop.CanWrite)
                    prop.SetValue(this, db[i]);
            }
        }

        // <summary>
        /// 数据表的列属性，使用 init 访问器使其只能在初始化时设置值。
        /// </summary>
        public string x1 { get; init; }  = string.Empty;
        public string x2 { get; init; } = string.Empty;
        [PrimaryKey]
        public string x3 { get; init; } = string.Empty;
        public string x4 { get; init; } = string.Empty;
        public string x5 { get; init; } = string.Empty;
        public string x6 { get; init; } = string.Empty;
        public string x7 { get; init; } = string.Empty;
        public string x8 { get; init; } = string.Empty;
        public string x9 { get; init; } = string.Empty;
        public string x10 { get; init; } = string.Empty;
        public string x11 { get; init; } = string.Empty;
        public string x12 { get; init; } = string.Empty;
        public string x13 { get; init; } = string.Empty;
        public string x14 { get; init; } = string.Empty;
        public string x15 { get; init; } = string.Empty;
        public string x16 { get; init; } = string.Empty;
        public string x17 { get; init; } = string.Empty;
        public string x18 { get; init; } = string.Empty;
        public string x19 { get; init; } = string.Empty;
        public string x20 { get; init; } = string.Empty;            
        public string x21 { get; init; } = string.Empty;
        public string x22 { get; init; } = string.Empty;

        internal string ToCsvLine()
        {
            string[] fields = new string[22];
            for (int i = 1; i <= 22; i++)
            {
                var prop = _props.FirstOrDefault(p => p.Name.Equals($"x{i}", StringComparison.OrdinalIgnoreCase));
                if (prop != null)
                {
                    fields[i-1] =  prop.GetValue(this)?.ToString() ?? string.Empty;
                }
            }

            return string.Join(",", fields.Select(f =>  $"\"{f}\"" ));
        }

        internal string getTitle()
        {
            
            string title = "原始输入,父订单号,支付单号,交易类型,创建时间,支付时间,结束时间,订单状态,卖家会员名,卖家2088ID,卖家会员ID,买家会员名,买家2088ID,买家会员ID,商品id,商品名称,交易数量,单价,成交金额,收货地址,退款金额,订单id";
            string[] fields = title.Split(",");

            return string.Join(",", fields.Select(f => $"\"{f}\""));
        }
    }

    public sealed record Buyer
    {
        public string x12 { get; init; } = string.Empty;
    }

}
